package com.yzw.ioTeach;

/**
 * @Author: YangZhenWei
 * @PROJECT_NAME: nettyTeach
 * @DESCRIPTION:
 * @USER: root
 * @DATE: 2023/8/26 12:01
 **/
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class BioDemo {
    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(8888);
        while (true) {
            Socket socket = serverSocket.accept();
            //启动一个新线程来处理客户端请求
            new Thread(new Handler(socket)).start();
        }
    }

    static class Handler implements Runnable {
        private Socket socket;

        public Handler(Socket socket) {
            this.socket = socket;
        }

        @Override
        public void run() {
            try {
                InputStream inputStream = socket.getInputStream();
                OutputStream outputStream = socket.getOutputStream();

                // 处理客户端请求
                // ...
//                System.out.println(inputStream.read());
                System.out.println(socket.getRemoteSocketAddress().toString()+"链接上了服务器"+socket.getLocalSocketAddress().toString());
//                System.out.println(socket.getLocalSocketAddress().toString());
//                inputStream.close();
//                outputStream.close();
//                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
